<template>
    <div v-if="roleKey === 'student' || roleKey === 'admin'">
<!--        走马灯-->
        <el-carousel :interval="4000" type="card" height="300px">
            <el-carousel-item v-for="(course, index) in courses" :key="index">
                <image-preview :src="course.imgs" class="carousel-image" alt="Course Image"/>
            </el-carousel-item>
        </el-carousel>
        <div class="featured-courses-title">可选课程</div>
<!--        <div class="featured-courses">
            <div class="course-card" v-for="course in courses" :key="course.id">
                <image-preview :src="course.imgs" class="course-image"/>
                <div class="course-content">
                    <h3 class="course-title">{{ course.name }}</h3>
                    <p class="course-description">{{ course.coursedesc }}</p>
                </div>
            </div>
        </div>-->
        <div class="featured-courses">
            <div class="course-card" v-for="course in courses" :key="course.id" @click="viewCourse(course)">
                <image-preview :src="course.imgs" class="course-image"/>
                <div class="course-content">
                    <h3 class="course-title">{{ course.name }}</h3>
                    <p class="course-description">{{ course.coursedesc }}</p>
                </div>
            </div>
        </div>

    </div>
</template>

<script setup>
    import { ref, defineExpose, onMounted } from 'vue';
    import { listMyCourse } from '@/api/MyCourse/MyCourse';
    import { getUserProfile } from '@/api/system/user'

    import { useRouter } from 'vue-router';
    const roleKey = ref('');
    const router = useRouter();
    const courses = ref([]);
    onMounted(() => {
        listMyCourse().then(response => {
            courses.value = response.rows;
        });
        getUserProfile().then(response => {
            // 确保 roles 数组存在且不为空
            if (Array.isArray(response.data.roles) && response.data.roles.length > 0) {
                // 获取第一个角色的 roleKey
                roleKey.value = response.data.roles[0].roleKey;
                console.log(roleKey.value)
            } else {
                console.error('Roles data is missing or invalid.');
            }
        });
    });
    defineExpose({
        getCourses: () => courses.value,
    });
    const viewCourse = (course) => {
        // 跳转到学生课程页面，携带课程ID
        router.push({
            path: '/classroom/studentCourse',
            query: {
                courseId: course.id, // 将课程ID作为查询参数
                permissions: ['studentCourse:studentCourse:list'], // 更新权限
            },
        });
    };

</script>

<style scoped>
    .el-carousel__item h3 {
        color: #475669;
        opacity: 0.75;
        /*line-height: 200px;*/
        margin: 0;
        text-align: center;
    }

    .el-carousel__item:nth-child(2n) {
        background-color: #99a9bf;
    }

    .el-carousel__item:nth-child(2n + 1) {
        background-color: #d3dce6;
    }
    .el-carousel__item {
        background-color: transparent; /* 移除默认的白色背景 */
        padding: 0; /* 移除默认的内边距 */
        box-shadow: none; /* 如果有阴影，移除阴影 */

    }
    .carousel-image{
        width: 100%; /* 确保图片宽度占满 carousel 宽度 */
        height: 100%; /* 确保图片高度占满 carousel 高度 */
        object-fit: cover; /* 保持图片比例，同时填满容器 */
    }


/*    */

    .featured-courses-title{
        text-align: center;
        font-size: 24px;
        margin-top: 30px;
        padding-bottom: 8px;
    /*    下边框一半，加粗*/
        border-bottom: 2px solid #333;
    }
    .featured-courses {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 20px;
        padding: 20px 0;
    }

    .course-card {
        display: flex;
        flex-direction: column;
        align-items: center;
        text-align: center;
        padding: 10px;
        border: 1px solid #ddd;
        border-radius: 4px;
        transition: all 0.3s ease-in-out;
    }

    .course-card:hover {
        transform: scale(1.1);
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }

    .course-image {
        width: 100%;
        height: 150px;
        object-fit: cover;
    }

    .course-content {
        padding: 10px;
    }

    .course-title {
        font-weight: bold;
        margin-bottom: 5px;
    }

    .course-description {
        font-size: 12px;
        color: #666;
    }
</style>